<template>
  <view class="w-all">
    <u-checkbox-group class="w-all">
      <block v-for="(v, index) in goodsList.goods_list" :key="index">
        <view class="flex justify-between u-p-y-30" style="width: 100%">
          <u-checkbox
            v-model="v.goodsChecked"
            shape="circle"
            :name="index"
            @change="checkboxChange"
            activeColor="#FF7405"
          ></u-checkbox>
          <view
            class="flex-1 flex align-center justify-between"
            style="width: 100%"
          >
            <image
              class="rounded-10"
              :src="v.images"
              style="width: 162rpx; height: 162rpx"
              mode="aspectFill"
            ></image>
            <view
              class="flex-1 flex flex-column justify-between u-p-x-20"
              style="height: 100%"
            >
              <text class="u-font-26 u-line-2">{{ v.goods_name }}</text>
              <text>{{ v.goods_sku.goods_attr || "默认规格" }}</text>
              <view class="flex align-center justify-between">
                <view class="font main-text-color">￥{{ v.goods_price }}</view>
                <dy-number-box
                  :item="v"
                  :num="v.total_num"
                  :disabled="disabled"
                  @plus="plus"
                  @minus="minus"
                ></dy-number-box>
              </view>
            </view>
          </view>
        </view>
      </block>
    </u-checkbox-group>
  </view>
</template>

<script>
import { mapMutations } from "vuex";
import { GoodsCenterApi } from "@/api/serves.js";
export default {
  name: "dy-supplier-carts-item",
  props: {
    goodsList: {
      type: Object,
      default: {},
    },
    goodsType: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      disabled: false,
    };
  },
  methods: {
    ...mapMutations("user", [
      "CHECKED_ONE",
      "GOODS_NUM_ADD",
      "GOODS_NUM_SUB",
      "CHECKED_ONE1",
      "GOODS_NUM_ADD1",
      "GOODS_NUM_SUB1",
    ]),
    // 店铺内商品选中
    checkboxChange(e) {
      // console.log(45454, e);
      if (this.goodsType == 1) {
        this.CHECKED_ONE({ shopId: this.goodsList.id, ...e });
      } else {
        this.CHECKED_ONE1({ shopId: this.goodsList.id, ...e });
      }
    },
    // 商品数量加 1 item 当前产品的信息对象
    plus(item) {
      let params = {
        goods_num: 1,
        goods_id: item.goods_id,
        goods_sku_id: item.goods_sku_id,
      };
      console.log("商品数量加 params", params);
      this.disabled = true;
      GoodsCenterApi.addCart(params).then((res) => {
        console.log("商品数量加", res);
        this.disabled = false;
        if (res.code == 1) {
          let obj = {
            id: this.goodsList.id, // 商户 id
            goods_id: item.goods_id,
            goods_sku_id: item.goods_sku_id, // 商品 id
          };
          if (this.goodsType == 1) {
			  this.GOODS_NUM_ADD(obj);
          }else{
			  this.GOODS_NUM_ADD1(obj);
		  }
        } else {
          this.$toast(res.msg);
        }
      });
    },
    // 商品数量减 1
    minus(item) {
      if (item.total_num == 1) return this.$Toast("商品数量不能为0");
      let params = {
        goods_num: 1,
        goods_id: item.goods_id,
        goods_sku_id: item.goods_sku_id,
      };
      this.disabled = true;
      GoodsCenterApi.subCart(params).then((res) => {
        console.log("商品数量减少", res);
        this.disabled = false;
        if (res.code == 1) {
          let obj = {
            id: this.goodsList.id, // 商户 id
            goods_id: item.goods_id,
            goods_sku_id: item.goods_sku_id, // 商品 id
          };
          if (this.goodsType == 1) {
			  this.GOODS_NUM_SUB(obj);
          }else{
			  this.GOODS_NUM_SUB1(obj);
		  }
        } else {
          this.$toast(res.msg);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
